package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.model.GetConsultantList.ConsultantList;
import com.emm.yixun.mobile.ui.signed.NewSignedActivity.JJInfo;
import com.eroad.product.tools.CLEditText;
import com.eroad.product.tools.PickerView;
import com.eroad.product.tools.PickerView.onSelectListener;

public class JJAdapter extends BaseAdapter {

	ArrayList<JJInfo> list;
	Activity context;
	ArrayList<ConsultantList> consultantList;
	private Integer index = -1;

	/** 0修改界面 1详情界面 */
	int type = 0;

	/** 推荐人分佣比例，默认为0 */
	private double referenceProportion = 0;

	public JJAdapter(ArrayList<JJInfo> list, Activity context, ArrayList<ConsultantList> consultantList) {
		this.list = list;
		this.context = context;
		this.consultantList = consultantList;
	}

	public void SetReferenceProportion(double referenceProportion) {
		this.referenceProportion = referenceProportion;
	}

	public void SetType(int type) {
		this.type = type;
	}

	public void SetDate(ArrayList<JJInfo> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return null == list ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return null == list ? null : list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHoudlerjj v = null;
		if (null == convertView) {
			convertView = LayoutInflater.from(context).inflate(R.layout.jjadapter_item, null);
			v = new ViewHoudlerjj();
			v.number = (CLEditText) convertView.findViewById(R.id.number);
			v.type_open = (TextView) convertView.findViewById(R.id.type_open);
			v.title_name = (TextView) convertView.findViewById(R.id.title_name);
			v.delete = (ImageView) convertView.findViewById(R.id.delete);
			v.top_tt = (ImageView) convertView.findViewById(R.id.top_tt);
			v.user1 = (LinearLayout) convertView.findViewById(R.id.user1);
			v.lines_user = convertView.findViewById(R.id.lines_user);
			convertView.setTag(v);
		} else {
			v = (ViewHoudlerjj) convertView.getTag();
			v.number.clearTextChangedListeners();
		}
		if (list.size() <= 1) {
			v.user1.setVisibility(View.GONE);
			v.lines_user.setVisibility(View.GONE);
		} else {
			v.user1.setVisibility(View.VISIBLE);
			v.lines_user.setVisibility(View.VISIBLE);
		}

		if (type == 0) {
//			Drawable right_arrow=context.getResources().getDrawable(R.drawable.right_arrow);  
//			right_arrow.setBounds(0, 0, right_arrow.getMinimumWidth(), right_arrow.getMinimumHeight());  
//			v.type_open.setCompoundDrawables(null, null, right_arrow, null); 
			v.top_tt.setVisibility(View.VISIBLE);
			v.type_open.setEnabled(true);
			v.number.setFocusable(true);
			v.number.setFocusableInTouchMode(true);
			v.delete.setVisibility(View.VISIBLE);
		} else {
//			v.type_open.setCompoundDrawables(null, null, null, null);
			v.top_tt.setVisibility(View.GONE);
			v.type_open.setEnabled(false);
			v.number.setFocusable(false);
			v.number.setFocusableInTouchMode(false);
			v.delete.setVisibility(View.INVISIBLE);
		}
		v.title_name.setText("经纪人" + (position + 1));
		v.delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				list.remove(position);
				notifyDataSetChanged();
			}
		});

		if (null != list.get(position).getPid()) {
			if (TextUtils.isEmpty(list.get(position).getName()) || "".equals(list.get(position).getName())
					|| null == list.get(position).getName()) {
				v.type_open.setText("");
			} else {
				v.type_open.setText(list.get(position).getName());
			}

			if (TextUtils.isEmpty(list.get(position).getNumber()) || "".equals(list.get(position).getNumber())
					|| null == list.get(position).getNumber()) {
				v.number.setText("");
			} else {
				v.number.setText(list.get(position).getNumber());
			}

		} else {
			v.type_open.setText("");
			v.number.setText("");
		}

		final TextView type_opend = v.type_open;
		v.type_open.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ArrayList<String> listname = new ArrayList<String>();
				if (null == consultantList || consultantList.size() <= 0) {
					EmmApplication.Ts("当前没有经纪人");
					return;
				}
				for (int i = 0; i < consultantList.size(); i++) {
					listname.add(consultantList.get(i).getConsultantUserName());
				}
				if (listname.size() <= 0) {
					EmmApplication.Ts("当前没有经纪人");
					return;
				} else {
					SetzpPopu(listname, "经纪人", type_opend, position);
				}
			}
		});

		final CLEditText ee = v.number;
		v.number.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (null != s.toString()) {
					list.get(position).setNumber(s.toString());
					double allprice = 0;
					for (int i = 0; i < list.size(); i++) {
						String str = (IsNull(list.get(i).getNumber()) == true ? list.get(i).getNumber() : "0");
						allprice += Double.valueOf(str).doubleValue();
					}
					allprice += referenceProportion;
					if (allprice > 100) {
						ee.setText(s.toString().substring(0, (s.toString().length() - 1)));
						ee.setSelection(ee.getText().toString().trim().length());
						list.get(position).setNumber(ee.getText().toString().trim());
						EmmApplication.Ts("分佣比例总和不能大于100%");
						return;
					}
				} else {
				}
			}
		});

		return convertView;
	}

	public static class ViewHoudlerjj {
		// EditText number;
		CLEditText number;
		TextView type_open, title_name;
		ImageView delete, top_tt;
		LinearLayout user1;
		View lines_user;
	}

	/** 判String断是否为空 */
	private boolean IsNull(String str) {
		if (null == str || "".equals(str) || TextUtils.isEmpty(str) || "null".equals(str)) {
			return false;
		} else {
			return true;
		}
	}

	/* ---公用选择框--- */
	View view;
	PopupWindow popu;
	/** 选中内容 */
	String selectName;
	/** 选中下标 */
	int SelectID;

	/* ---公用选择框--- */

	/** 选项框 */
	public void SetzpPopu(final List<String> listname, String title, final TextView ed, final int p) {
		view = LayoutInflater.from(context).inflate(R.layout.popu_zp, null);
		popu = new PopupWindow(view, android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT,
				android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT);
		popu.setFocusable(true);
		// EmmApplication.SetNumberLog(context, 0.5f);// 改变popupwindow范围外的屏幕亮度
		popu.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
		popu.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		popu.showAtLocation(view, Gravity.BOTTOM, 0, 0);
		popu.showAsDropDown(view, 0, 0);
		popu.setFocusable(true);
		popu.setOutsideTouchable(true);
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					popu.dismiss();
					EmmApplication.setLog(context);
				}
				return false;
			}
		});
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				popu.dismiss();
				EmmApplication.setLog(context);
			}
		});

		PickerView select_pick = (PickerView) view.findViewById(R.id.select_pick);

		selectName = listname.get(0);

		TextView shuerbtn = (TextView) view.findViewById(R.id.shuerbtn);
		RelativeLayout relative_all = (RelativeLayout) view.findViewById(R.id.relative_all);
		ImageView dis_btn = (ImageView) view.findViewById(R.id.dis_btn);
		dis_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu.dismiss();
				EmmApplication.setLog(context);
			}
		});
		TextView title_select = (TextView) view.findViewById(R.id.title_select);

		title_select.setText(title);

		select_pick.setData(listname);
		select_pick.setOnSelectListener(new onSelectListener() {
			@Override
			public void onSelect(String text, int id) {
				selectName = text;
				SelectID = id;
			}
		});
		select_pick.setSelected(0);
		shuerbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 确定
				popu.dismiss();
				EmmApplication.setLog(context);
				// EmmApplication.T("选择:" + selectName + "==>ID:" + SelectID);

				try {
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).getName().equals(selectName)) {
							EmmApplication.Ts("该经纪人已在分佣列表内");
							return;
						}
					}
				} catch (NullPointerException e) {
				}

				ed.setText(selectName);
				list.get(p).setName(selectName);
				for (int i = 0; i < consultantList.size(); i++) {
					if (consultantList.get(i).getConsultantUserName().equals(selectName)) {
						list.get(p).setPid(consultantList.get(i).getConsultantUserId());
						Log.d("-------1--", consultantList.get(i).getConsultantUserId());
						Log.d("-------2--", list.get(p).getPid());
					}
				}
			}
		});
		relative_all.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
	}
}
